package com.yehaikeji.workflow.service.serviceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.github.pagehelper.util.StringUtil;
import com.yehaikeji.workflow.bean.incident;
import com.yehaikeji.workflow.bean.reason;
import com.yehaikeji.workflow.bean.thing;
import com.yehaikeji.workflow.bean.unit;
import com.yehaikeji.workflow.bean.user;
import com.yehaikeji.workflow.mapper.incidentMapper;
import com.yehaikeji.workflow.mapper.reasonMapper;
import com.yehaikeji.workflow.mapper.thingMapper;
import com.yehaikeji.workflow.mapper.unitMapper;
import com.yehaikeji.workflow.mapper.userMapper;
import com.yehaikeji.workflow.service.incidentService;

@Service
public class incidentServiceImpl implements incidentService {
	@Autowired
	private incidentMapper incidentMapper;
	@Autowired
	private userMapper userMapper;
	@Autowired
	private unitMapper unitMapper;
	@Autowired
	private thingMapper thingMapper;
	@Autowired
	private reasonMapper reasonMapper;

	@Override
	public int signature(String incidentid, String signatures, String signaturea) {
		incident incident = incidentMapper.selectByPrimaryKey(incidentid);
		if (incident == null) {
			throw new RuntimeException("参数错误");
		}
		thing thing = thingMapper.selectByPrimaryKey(incident.getThingid());
		thing.setUpdatedate(new Date());
		thing.setStatus(incident.getStatus());
		thingMapper.updateByPrimaryKeySelective(thing);

		incident.setIncidentid(incidentid);
		incident.setSignatures(signatures);
		incident.setSignaturea(signaturea);
		incident.setUpdatedate(new Date());
		return incidentMapper.updateByPrimaryKeySelective(incident);
	}

	@Override
	public String insertnextincident(incident incident) {
		List<incident> list = incidentMapper.selectBythingidlist(incident.getThingid());// 已经签名
		List<incident> incidents = incidentMapper.selectBythingidlistnull(incident.getThingid());// 未签名
		thing thing = thingMapper.selectByPrimaryKey(incident.getThingid());

//		if (CollectionUtils.isEmpty(list) || !CollectionUtils.isEmpty(incidents)) {
//			throw new RuntimeException("thingid错误或者上一步未签名");
//		}

		int statuss = incident.getStatus() - thing.getStatus();
		if (thing.getStatus() == 2 && (thing.getTypeid() == 1 || thing.getTypeid() == 4)) {
			if (incident.getStatus() != 3 && incident.getStatus() != 4 && incident.getStatus() != 6) {
				throw new RuntimeException("status错误2");
			}
		} else if (thing.getStatus() == 9 || statuss != 1 || thing.getStatus() == 3 || thing.getStatus() == 5
				|| thing.getStatus() == 7) {
			throw new RuntimeException("status错误1");
		}

		user user = userMapper.selectByPrimaryKey(incident.getUserid());
//		if (user == null) {
//			throw new RuntimeException("账号错误");
//		}
		if (!StringUtil.isEmpty(incident.getUnitidb())) {
			unit unit = unitMapper.selectByPrimaryKey(incident.getUnitidb());
			if (unit == null) {
				throw new RuntimeException("单位参数错误");
			}
			incident.setAccepetunit(unit.getName());
		}
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		if (!StringUtil.isEmpty(incident.getReason()) && incident.getStatus() == 6) {
			reason record = new reason();
			record.setContent(incident.getReason());
			record.setThingid(incident.getThingid());
			record.setUserid(incident.getUserid());
			reasonMapper.insertSelective(record);
		}else if(incident.getStatus() == 6&&StringUtil.isEmpty(incident.getReason())) {
			throw new RuntimeException("理由不能为空");
		}
		incident.setUnitida(null);
		incident.setIncidentid(uuid);
		incidentMapper.insertSelective(incident);

		return uuid;
	}
	
	@Override
	public String insertnextincidenta(incident incident) {
		List<incident> list = incidentMapper.selectBythingidlist(incident.getThingid());// 已经签名
		List<incident> incidents = incidentMapper.selectBythingidlistnull(incident.getThingid());// 未签名
		thing thing = thingMapper.selectByPrimaryKey(incident.getThingid());
		
//		if (CollectionUtils.isEmpty(list) || !CollectionUtils.isEmpty(incidents)) {
//			throw new RuntimeException("thingid错误或者上一步未签名");
//		}
		
		int statuss = incident.getStatus() - thing.getStatus();
		if (thing.getStatus() == 2 && (thing.getTypeid() == 1 || thing.getTypeid() == 4)) {
			if (incident.getStatus() != 3 && incident.getStatus() != 4 && incident.getStatus() != 6) {
				throw new RuntimeException("status错误2");
			}
		} else if (thing.getStatus() == 9 || statuss != 1 || thing.getStatus() == 3 || thing.getStatus() == 5
				|| thing.getStatus() == 7) {
			throw new RuntimeException("status错误1");
		}

		user user = userMapper.selectByPrimaryKey(incident.getUserid());
//		if (user == null) {
//			throw new RuntimeException("账号错误");
//		}
		if (!StringUtil.isEmpty(incident.getUnitidb())) {
			unit unit = unitMapper.selectByPrimaryKey(incident.getUnitidb());
			if (unit == null) {
				throw new RuntimeException("单位参数错误");
			}
			incident.setAccepetunit(unit.getName());
		}
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		if (!StringUtil.isEmpty(incident.getReason()) && incident.getStatus() == 6) {
			reason record = new reason();
			record.setContent(incident.getReason());
			record.setThingid(incident.getThingid());
			record.setUserid(incident.getUserid());
			reasonMapper.insertSelective(record);
		}else if(incident.getStatus() == 6&&StringUtil.isEmpty(incident.getReason())) {
			throw new RuntimeException("理由不能为空");
		}
		incident.setUnitida(null);
		incident.setIncidentid(uuid);
		incidentMapper.insertSelective(incident);
		thing.setUpdatedate(new Date());
		thing.setStatus(incident.getStatus());
		thingMapper.updateByPrimaryKeySelective(thing);
		return uuid;
	}

	@Override
	public List<Integer> incidentselectstatus(String thingid) {
		List<Integer> list = new ArrayList<>();
		List<incident> incidents = incidentMapper.selectBythingidlistnull(thingid);// 未签名
		
		thing thing=thingMapper.selectByPrimaryKey(thingid);
		if (thing==null) {

		} else {
			Map<String, Object> map=new HashMap<>();
			map.put("thingid", thingid);
			map.put("status", thing.getStatus());
			incidentMapper.deleteuselessincident(map);
			if ((thing.getTypeid()==4||thing.getTypeid()==1)&&thing.getStatus() == 2) {
				list.add(3);
				list.add(4);
				list.add(6);
			}else if(thing.getTypeid()==2&&thing.getStatus() == 2) {
				
			}else if(thing.getStatus()!=3&&thing.getStatus()!=5&&thing.getStatus()!=7&&thing.getStatus()!=9){
				list.add(thing.getStatus() + 1);
			}
		}
		return list;
	}

}
